package com.example.stock.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.stock.comm.StrategyEnum;
import com.example.stock.entity.eo.StockTradeEo;
import com.example.stock.entity.mapper.StockTradeMapper;
import com.example.stock.vo.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.util.*;

@RestController
@RequestMapping("/api/common")
public class ViewController {
  @Resource
  private StockTradeMapper stockTradeMapper;

  public Object getScore(String tsCode,String strategy){
    ScoreVo scoreVo = new ScoreVo();
    scoreVo.setScore(0.0D);


    List<StockTradeEo> stockTradeEos = stockTradeMapper.selectList(Wrappers.<StockTradeEo>lambdaQuery()
            .eq(StockTradeEo::getStrategy, StrategyEnum.getByNameExt(strategy).name())
            .eq(StockTradeEo::getTsCode, tsCode)
            .orderByAsc(StockTradeEo::getHoldTime));

    StockTradeEo firstTrade = stockTradeEos.get(0);
    StockTradeEo lastedTrade = stockTradeEos.get(stockTradeEos.size() - 1);

    scoreVo.setStartTime(firstTrade.getHoldTime());
    scoreVo.setEndTime(lastedTrade.getHoldTime());



    scoreVo.setReturnRateTotalMy(getDouble((lastedTrade.getMoney() - firstTrade.getMoney()) / firstTrade.getMoney() * 100));
    scoreVo.setReturnRateTotalIndex(getDouble((lastedTrade.getPrice() - firstTrade.getPrice()) / firstTrade.getPrice() * 100));
    scoreVo.setReturnRateTotalRelative(scoreVo.getReturnRateTotalMy() - scoreVo.getReturnRateTotalIndex());

    long days = Duration.between(firstTrade.getHoldTime(), lastedTrade.getHoldTime()).toDays();
    System.out.println(days);

    //TODO 简单的球平均值
    scoreVo.setReturnRateYearMy(scoreVo.getReturnRateTotalMy() * 365 / days );
    scoreVo.setReturnRateYearIndex(scoreVo.getReturnRateTotalIndex() * 365 / days);
    scoreVo.setReturnRateYearRelative(scoreVo.getReturnRateTotalRelative() * 365 / days);

    double maxDrawdown = 0.0;
    StockTradeEo temp = firstTrade;
    StockTradeEo startDrawDown = firstTrade;
    StockTradeEo endDrawDown = firstTrade;
    for (StockTradeEo currentTrade : stockTradeEos) {
      double earnMoney = currentTrade.getMoney() - temp.getMoney();
      if (earnMoney >= 0) {
        temp = currentTrade;
        continue;
      }
      double currentDrawDown = (earnMoney / temp.getMoney())*100;
      if (currentDrawDown < maxDrawdown) {
        maxDrawdown = currentDrawDown;
        startDrawDown = temp;
        endDrawDown = currentTrade;
      }

    }

    scoreVo.setDrawdownMe(getDouble(maxDrawdown));
    scoreVo.setDrawdownIndex(getDouble( (endDrawDown.getPrice()-startDrawDown.getPrice())/startDrawDown.getPrice() *100 ));
    scoreVo.setDrawdownStartTime(startDrawDown.getHoldTime());
    scoreVo.setDrawdownEndTime(endDrawDown.getHoldTime());

    return scoreVo;
  }

  private double getDouble(double v) {
    return new BigDecimal(v).setScale(3, RoundingMode.HALF_UP).doubleValue();
  }
}